printing: Do not truncate job names in GtkPrintOperation
authorCarlos Garcia Campos <cgarcia@igalia.com>
Tue, 8 Nov 2016 10:44:44 +0000 (11:44 +0100)
committerCarlos Garcia Campos <carlosgc@gnome.org>
Thu, 10 Nov 2016 15:57:30 +0000 (16:57 +0100)
We are currently truncating job names to 255 bytes, because that's the
maximum allowed length of job-name attribute in CUPS. This is a CUPS
limitation that GtkPrintOperation shouldn't need to know, and it
shouldn't affect other backends, that might have other limitations or
even no limitation at all. This has another side effect, that what you
set as GtkPrintOperation:job-name could be different to what you get if
the property is truncated, this is not documented in
gtk_print_operation_set_job_name(). So, I think the job name should be
truncated by the CUPS backend, right before setting the job-name
attribute.

https://bugzilla.gnome.org/show_bug.cgi?id=774097

gtk/gtkprintoperation.c
modules/printbackends/cups/gtkprintbackendcups.c

index db53d050cc7c7ac56cb585c66cdda493dc371d03..329a07aeaf817696b3e908860813638373e10646 100644 (file)
@@ -1606,33 +1606,17 @@ gtk_print_operation_set_job_name (GtkPrintOperation *op,
                                  const gchar       *job_name)
 {
   GtkPrintOperationPrivate *priv;
-  gchar *end;
 
   g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
   g_return_if_fail (job_name != NULL);
 
   priv = op->priv;
 
-  g_free (priv->job_name);
-  /*
-   * according to http://datatracker.ietf.org/doc/rfc2911/,
-   * job names MUST NOT exceed 255 (MAX) octets.
-   *
-   * CUPS will not print jobs with names exceeding 255 chars.
-   */
-  if (strlen (job_name) > 255)
-    {
-      end = g_utf8_find_prev_char (job_name, job_name + 255);
-      priv->job_name = g_utf8_substring (job_name,
-                                         0,
-                                         g_utf8_pointer_to_offset (job_name,
-                                                                   end));
-    }
-  else
-    {
-      priv->job_name = g_strdup (job_name);
-    }
+  if (g_strcmp0 (priv->job_name, job_name) == 0)
+    return;
 
+  g_free (priv->job_name);
+  priv->job_name = g_strdup (job_name);
 
   g_object_notify (G_OBJECT (op), "job-name");
 }
index d1140764a797d757a1ea8202878878e13f9128fb..2056434beaea051bd09cdc14692696662904a8a8 100644 (file)
@@ -752,10 +752,26 @@ gtk_print_backend_cups_print_stream (GtkPrintBackend         *print_backend,
                                    NULL, printer_absolute_uri);
 
   title = gtk_print_job_get_title (job);
-  if (title)
+  if (title) {
+    char *title_truncated = NULL;
+    size_t title_bytes = strlen (title);
+
+    if (title_bytes >= IPP_MAX_NAME)
+      {
+        gchar *end;
+
+        end = g_utf8_find_prev_char (title, title + IPP_MAX_NAME - 1);
+        title_truncated = g_utf8_substring (title,
+                                            0,
+                                            g_utf8_pointer_to_offset (title, end));
+      }
+
     gtk_cups_request_ipp_add_string (request, IPP_TAG_OPERATION,
                                      IPP_TAG_NAME, "job-name",
-                                     NULL, title);
+                                     NULL,
+                                     title_truncated ? title_truncated : title);
+    g_free (title_truncated);
+  }
 
   options_data = g_new0 (CupsOptionsData, 1);
   options_data->request = request;